package com.hsmw.api.controller.biz;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hsmw.api.service.*;
import com.hsmw.api.vo.ConcreteCountTypeVO;
import com.hsmw.api.vo.ConcretePlantInfoVO;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.htn.common.core.constant.HtnUserRoleEnum;
import com.htn.common.core.constant.UserTypeEnum;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @author WD
 */
@Api(tags = "BIZ-搅拌站信息统计")
@RestController
@RequestMapping("/api/concrete/plant")
public class BizConcretePlantController extends BaseController<HsmwConcretePlantInfoService, HsmwConcretePlantInfo> {

    @Autowired
    private HsmwConcretePlantInfoService hsmwConcretePlantInfoService;

    @Autowired
    private HsmwOrderService hsmwOrderService;

    @Autowired
    private HsmwVehicleService hsmwVehicleService;

    @Autowired
    private HsmwPmUserService hsmwPmUserService;

    @Autowired
    private HsmwConcreteUserService hsmwConcreteUserService;

    @Autowired
    private HsmwDriverUserService hsmwDriverUserService;

    @Autowired
    private HsmwBizUserService hsmwBizUserService;

    @Autowired
    private HsmwHtnUserService hsmwHtnUserService;

    @Autowired
    private HsmwTpspUserService hsmwTpspUserService;

    @Autowired
    private HsmwSaleUserService hsmwSaleUserService;





    /*
     * 分页
     *
     * @param basePage
     * @return
     */
    @ApiOperation(value = "获取搅拌站分页列表，参数： 查询参数 queryParam ", notes = "获取司机分页列表", produces = "application/json")
    @PostMapping("/getHsmwConcretePlantPage")
    public DataResponse getHsmwConcretePlantPage(@RequestBody @Valid BasePage basePage) {
        IPage<ConcretePlantInfoVO> data = hsmwConcretePlantInfoService.getConcretePlantList(basePage);
        return DataResponse.success(data);
    }


    @ApiOperation(value = "新增搅拌站 ", notes = "新增搅拌站", produces = "application/json")
    @PostMapping("/addConcretePlantInfo")
    public DataResponse addConcretePlantInfo(@RequestBody HsmwConcretePlantInfo hsmwConcretePlantInfo) {
        List<HsmwHtnUser> users = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
                .eq(HsmwHtnUser::getUsername,hsmwConcretePlantInfo.getUsername()));
        if(CollectionUtil.isNotEmpty(users)){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL, "负责人电话在已经存在，请重新输入！");
        }

        if (hsmwConcretePlantInfo == null) {
            return new DataResponse(CommonConstants.ResponseStatus.FAIL, "参数传递错误");
        }
        if (StringUtils.isNotBlank(hsmwConcretePlantInfo.getPlantName())) {
            List<HsmwConcretePlantInfo> list = hsmwConcretePlantInfoService.list(new LambdaQueryWrapper<HsmwConcretePlantInfo>()
                    .eq(HsmwConcretePlantInfo::getPlantName, hsmwConcretePlantInfo.getPlantName()));
            if (CollectionUtil.isNotEmpty(list)) {
                return new DataResponse(CommonConstants.ResponseStatus.FAIL, "搅拌站名称不可重复");
            }
        }
        HsmwHtnUser hsmwUser = new HsmwHtnUser();
        hsmwUser.setFlag(false);
        hsmwUser.setUsername(hsmwConcretePlantInfo.getUsername());
        hsmwUser.setName(hsmwConcretePlantInfo.getName());
        hsmwUser.setType(UserTypeEnum.CONCRETE.getValue());
        hsmwUser.setEnabled(true);
        hsmwUser.setRoleType(HtnUserRoleEnum.LEADING.getValue());
        hsmwUser.setLastLoginTime(LocalDateTime.now());
        String  concreteId = CommonUtils.generateId(hsmwConcretePlantInfo);
        hsmwUser.setOwnCompanyId(concreteId);
        hsmwUser.setOwnCompanyName(hsmwConcretePlantInfo.getName());
        hsmwHtnUserService.save(hsmwUser);
        hsmwUser = hsmwHtnUserService.findByUsername(hsmwConcretePlantInfo.getUsername());
        hsmwConcretePlantInfo.setUserId(hsmwUser.getId());
        hsmwConcretePlantInfoService.save(hsmwConcretePlantInfo);
        return new DataResponse();
    }


    /*
     * 获取搅拌站详情和 统计数据和混凝土订单

     * @param basePage
     * @return
     */
    @ApiOperation(value = "获取搅拌站详情和 统计数据和混凝土订单  参数： plantId 搅拌站ID", notes = "获取搅拌站详情和", produces = "application/json")
    @PostMapping("/getConcretePlantDetail")
    public DataResponse getHsmwConcretePlantPage(String  plantId) {
        ConcretePlantInfoVO  data = hsmwConcretePlantInfoService.getConcretePlantDetail(plantId);
        return DataResponse.success(data);
    }


    /*
     * 分页
     * @param basePage
     * @return
     */
    @ApiOperation(value = "获取搅拌站 按照月和年统计 数据 ", notes = "获取司机分页列表", produces = "application/json")
    @ApiImplicitParam(name = "type",value = "统计纬度 月 month  年 year",dataType = "String")
    @PostMapping("/getConcreteStatistics")
    public DataResponse getConcreteStatistics(@RequestBody ConcreteCountTypeVO concreteCountTypeVO) {
        if(StringUtils.isBlank(concreteCountTypeVO.getType())){
            return  new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED,"您输入的统计类型为空！");
        }
        if(StringUtils.isBlank(concreteCountTypeVO.getPlantId())){
            return  new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED,"您输入的搅拌站ID为空！");
        }
        HashMap data = hsmwConcretePlantInfoService.getConcreteStatistics(concreteCountTypeVO);
        return DataResponse.success(data);
    }



    /**
     *
     * @param
     * @return
     */
    @ApiOperation(value = "根据混凝土订单ID 查询 详情", notes = "根据混凝土订单ID 查询 详情", produces = "application/json")
    @ApiImplicitParam(name = "orderId",value = "订单状态 1：等待接单 2：等待出发 3： 已出发 4： 已到达  6： 已完成 7 已确认 8 已取消",dataType = "String")
    @PostMapping("/getConcreteOrderList")
    public DataResponse getConcreteOrderList(String  orderId) {
        HashMap data = hsmwOrderService.getHsmwOrderDetailByOrderId(orderId);
        return new DataResponse(data);
    }


    @ApiOperation(value = "修改搅拌站 ", notes = "修改搅拌站", produces = "application/json")
    @PostMapping("/updateConcretePlantInfo")
    public DataResponse updateConcretePlantInfo(@RequestBody HsmwConcretePlantInfo hsmwConcretePlantInfo){

        if(hsmwConcretePlantInfo == null){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"传递参数出现问题，请检查！");
        }
        if(StringUtils.isBlank(hsmwConcretePlantInfo.getId())){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"id不可为空");
        }

        HsmwConcretePlantInfo plantInfo = hsmwConcretePlantInfoService.getById(hsmwConcretePlantInfo.getId());
        String username = hsmwConcretePlantInfo.getUsername();
        HsmwHtnUser one = hsmwHtnUserService.getOne(new LambdaQueryWrapper<HsmwHtnUser>()
                .eq(HsmwHtnUser::getUsername, username));
        if(StringUtils.isNotNull(one) && !StringUtils.equals(one.getId(),plantInfo.getUserId())){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"手机号重复");
        }
        HsmwHtnUser user = hsmwHtnUserService.getById(plantInfo.getUserId());
        if(StringUtils.isNotNull(user)){
            user.setUsername(username);
            if(StringUtils.isNotBlank(hsmwConcretePlantInfo.getName())){
                user.setName(hsmwConcretePlantInfo.getName());
            }
            hsmwHtnUserService.updateById(user);
        }

        boolean upda = hsmwConcretePlantInfoService.updateById(hsmwConcretePlantInfo);

        if(upda){
            return new DataResponse(CommonConstants.ResponseStatus.SUCCESS,"修改成功");
        }
        return new DataResponse(CommonConstants.ResponseStatus.FAIL,"修改失败");
    }


    @ApiOperation(value = "删除搅拌站 ", notes = "删除搅拌站", produces = "application/json")
    @PostMapping("/deleteConcretePlantInfoById")
    public DataResponse deleteConcretePlantInfoById(@RequestBody GetObjectByIdForm form){

        if(form.getKey() == null){
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"id不能为空");
        }
        boolean del = hsmwConcretePlantInfoService.removeById(form.getKey());
        if(del){
            return new DataResponse(CommonConstants.ResponseStatus.SUCCESS,"删除成功");
        }
        return new DataResponse(CommonConstants.ResponseStatus.FAIL,"删除失败");

    }

}
